package com.woniu.user.controller;


import com.woniu.user.commons.Result;
import com.woniu.user.model.Pic;
import com.woniu.user.service.PicService;
import com.woniu.user.util.JwtUtil;
import com.woniu.user.util.OSSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 场馆图片表 前端控制器
 * </p>
 *
 * @author Silas
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/pic")
@Slf4j
@Api(tags = "上传文件")
public class PicController {

    @Resource
    private PicService picService;

    @PostMapping("/upload")
    @ApiOperation("上传文件")
    public Result upload(MultipartFile file, HttpServletRequest req) throws Exception {
        log.info("执行了上传文件操作 {}", file);
        String filename = file.getOriginalFilename();
        String key = System.currentTimeMillis() + filename.substring(filename.lastIndexOf("."));
        //写入OSS对象储存
        String url = OSSUtil.uploadFile(file.getInputStream(), key, "silas103");
        //写入数据库
        Integer venueId = JwtUtil.getVenueIdFromReq(req);

        Pic pic = new Pic();
        pic.setVenueId(venueId);
        pic.setLink(url);
        picService.save(pic);
        System.out.println(url);
        return Result.success(200, "上传成功", url);

    }
}

